﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System;
using PenguinForce.Collisions;

namespace PenguinForce.Entities
{
    /// <summary>
    /// base class for moveable, drawable critters onscreen
    /// </summary>
    public class Entity
    {
        #region Variables

        /// <summary>
        /// texture drawn to represent this animal
        /// </summary>
        protected Texture2D _texture;

        /// <summary>
        /// tint color to draw the texture with
        /// </summary>
        protected Color _color = Color.White;

        /// <summary>
        /// center of the draw texture
        /// </summary>
        protected Vector2 _textureCenter;

        /// <summary>
        /// movement speed in pixels/second
        /// </summary>
        protected float _moveSpeed;

        /// <summary>
        /// All the behavior that this animal has
        /// </summary>
        protected Dictionary<EntityType, FlockBehaviors.Behaviors> _behaviors;

        protected EntityType _animaltype = EntityType.Generic;

        protected float _reactionDistance;

        protected Vector2 _reactionLocation;

        protected bool _fleeing = false;

        protected Vector2 _direction;

        protected Vector2 _location;

        #endregion

        #region Constructors

        /// <summary>
        /// Sets the boundries the animal can move in the texture used in Draw
        /// </summary>
        /// <param name="theTex">Texture to use</param>
        /// <param name="screenSize">Size of the sample screen</param>
        public Entity(Texture2D theTex)
        {
            InitializeTexture(theTex);
            _moveSpeed = 0.0f;

            _behaviors = new Dictionary<EntityType, FlockBehaviors.Behaviors>();
        }

        #region Properties

        /// <summary>
        /// The animal type
        /// </summary>
        public EntityType AnimalType
        {
            get
            {
                return _animaltype;
            }
        }

        /// <summary>
        /// Reaction distance
        /// </summary>
        public float ReactionDistance
        {
            get
            {
                return _reactionDistance;
            }
        }

        /// <summary>
        /// Reaction location
        /// </summary>
        public Vector2 ReactionLocation
        {
            get
            {
                return _reactionLocation;
            }
        }

        public bool Fleeing
        {
            get
            {
                return _fleeing;
            }
            set
            {
                _fleeing = value;
            }
        }

        /// <summary>
        /// Direction the animal is moving in
        /// </summary>
        public Vector2 Direction
        {
            get
            {
                return _direction;
            }
            set
            {
                _direction = value;
            }
        }

        /// <summary>
        /// Location on screen
        /// </summary>
        public Vector2 Location
        {
            get
            {
                return _location;
            }
            set
            {
                _location = value;
            }
        }

        #endregion

        #endregion

        #region Methods

        protected void InitializeTexture(Texture2D theTex)
        {
            if (theTex != null)
            {
                _texture = theTex;
                _textureCenter = new Vector2(_texture.Width / 2, _texture.Height / 2);
            }
        }

        /// <summary>
        /// Empty update function
        /// </summary>
        /// <param name="gameTime"></param>
        public virtual void Update(GameTime gameTime)
        {
        }

        /// <summary>
        /// Draw the Animal with the specified SpriteBatch
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="gameTime"></param>
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            float rotation = (float)Math.Atan2(_direction.Y, _direction.X);

            spriteBatch.Draw(_texture, _location, null, _color, rotation, _textureCenter, 1.0f, SpriteEffects.None, 0.0f);
        }

        #endregion
    }
}